package com.supermap.geometry.clientservice;

import com.supermap.geometry.service.GetDatasetGrpc;
import com.supermap.geometry.service.GetDatasetReply;
import com.supermap.geometry.service.GetDatasetRequest;
import com.supermap.grpc.HelloWorldClient;
import com.supermap.protos.geometry.Dataset;
import io.grpc.StatusRuntimeException;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author liqs
 * @version 1.0
 * @date 2021/10/21 11:32
 */
public class ClientService {

    private final Logger logger = Logger.getLogger(ClientService.class.getName());

    public ClientService() {}

    public Dataset.ProtoDataset getDatasetByName(GetDatasetGrpc.GetDatasetBlockingStub getDatasetBlockingStub, String datasetname) {
        logger.info("datasetname is  " + datasetname);
        GetDatasetRequest getDatasetRequest = GetDatasetRequest.newBuilder().setDatasetname(datasetname).build();
        GetDatasetReply getDatasetReply;
        try {
            getDatasetReply = getDatasetBlockingStub.getDataset(getDatasetRequest);
        } catch (StatusRuntimeException e) {
            logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus());
            return null;
        }
        Dataset.ProtoDataset protoDataset = getDatasetReply.getProtoDataset();
        // 对返回数据的数据长度做检查，若缺失则直接报错
        if (protoDataset.toByteArray().length == getDatasetReply.getDatasetlength()) {
            return protoDataset;
        } else {
            logger.log(Level.WARNING, "信息传输长度不对，请重新获取");
            return null;
        }
    }

}
